/**
 * @Description: 业务功能管理
 * @Date 2017/12/5 9：30
 * @Authoer: weimei
 */
define([
  './themes/lang/' + window.YUFP_SYS_CONFIG.language + '/pages/content/systemManager/functionManage/functionManage.js'
], function (require, exports) {
  // page加载完成后调用ready方法
  exports.ready = function (hashCode, data, cite) {
    yufp.lookup.reg('YESNO');
    yufp.custom.vue({
      el: '#functionManage',
      data: function () {
        var me = this;
        // 业务功能url格式校验
        var urlValidate = function (rule, value, callback) {
          // var reg = /^pages\/([a-zA-Z0-9_]+\/)+[a-zA-Z0-9_]+$/;
          var reg = /^pages\/([a-zA-Z0-9_]+\/)+[a-zA-Z0-9_]+(\?([a-zA-Z0-9_]+=.+))?$/;
          if (!reg.test(value)) {
            callback(new Error(me.$t('functionManage.qsrhfgs')));
            return;
          }
          callback();
        };
        // 业务功能顺序校验
        var orderValidate = function (rule, value, callback) {
          var reg = /^\d{0,4}$/;
          if (!reg.test(value)) {
            callback(new Error(me.$t('functionManage.qsrzs')));
            return;
          }
          callback();
        };
        return {
          modFormdata: {},
          funcFormdata: {},
          viewType: 'DETAIL',
          viewTitle: yufp.lookup.find('CRUD_TYPE', false),
          iconDialogVisible: false,
          modCurrentRow: '',
          height: yufp.frame.size().height - 260,
          modFormDisabled: false,
          funcFormDisabled: false,
          modDialogVisible: false,
          funcDialogVisible: false,
          modAddCheck: !yufp.session.checkCtrl('add_mod'), // 新增模块按钮控制
          modModifyCheck: !yufp.session.checkCtrl('modify_mod'), // 模块修改按钮控制
          modDeleteCheck: !yufp.session.checkCtrl('delete_mod'), // 模块删除按钮控制
          funcAddCheck: !yufp.session.checkCtrl('add_func'), // 业务功能新增按钮控制
          funcModifyCheck: !yufp.session.checkCtrl('modify_func'), // 业务功能修改按钮控制
          funcDeleteCheck: !yufp.session.checkCtrl('delete_func'), // 业务功能删除按钮控制
          modDataUrl: backend.appOcaService + '/api/adminsmfuncmod/querymod',
          funcDataUrl: backend.appOcaService + '/api/adminsmbusifunc/queryfunc',
          icons: yufp.util.icons(),
          modTableColumns: [
            {
              label: this.$t('functionManage.mkmc'),
              prop: 'modName',
              sortable: true,
              resizable: true,
              template: function () {
                return '<template scope="scope">\
                                <a onclick="return false;" href="javascipt:void(0);" class="underline" style="text-decoration:underline;" @click="_$event(\'custom-row-click\', scope)">{{ scope.row.modName }}</a>\
                            </template>';
              }
            }
          ],
          funcTableColumns: [
            {
              label: this.$t('functionManage.ywgnmc'),
              prop: 'funcName',
              width: 250,
              sortable: true,
              resizable: true,
              template: function () {
                return '<template scope="scope">\
                                <a onclick="return false;" href="javascipt:void(0);" class="underline" style="text-decoration:underline;" @click="_$event(\'custom-row-click\', scope)">{{ scope.row.funcName }}</a>\
                            </template>';
              }
            },
            {
              label: this.$t('functionManage.lj'),
              prop: 'funcUrl',
              width: 275,
              sortable: true,
              resizable: true,
              showOverflowTooltip: true
            },
            { label: this.$t('functionManage.sx'), prop: 'funcOrder', sortable: true, resizable: true }
          ],
          modFields: [{
            columnCount: 1,
            fields: [
              {
                field: 'modName',
                label: this.$t('functionManage.mkmc'),
                rules: [
                  { required: true, message: this.$t('functionManage.btx'), trigger: 'blur' },
                  { max: 20, message: this.$t('functionManage.srzgc'), trigger: 'blur' },
                  { validator: yufp.validator.speChar, message: this.$t('functionManage.srxxbhtszf'), trigger: 'blur' }
                ]
              }
            ]
          }, {
            columnCount: 2,
            fields: [
              {
                field: 'isApp',
                label: this.$t('functionManage.sfgn'),
                type: 'select',
                dataCode: 'YESNO',
                rules: [{ required: true, message: this.$t('functionManage.btx'), trigger: 'blur' }]
              },
              {
                field: 'isOuter',
                label: this.$t('functionManage.sfwbxt'),
                type: 'select',
                dataCode: 'YESNO',
                rules: [{ required: true, message: this.$t('functionManage.btx'), trigger: 'blur' }],
                change: function (isOuter) {
                  if (isOuter == 'Y') {
                    me.$refs.modForm.switch('userName', 'disabled', false);
                    me.$refs.modForm.switch('userKey', 'disabled', false);
                    me.$refs.modForm.switch('password', 'disabled', false);
                    me.$refs.modForm.switch('pwdKey', 'disabled', false);
                  } else {
                    me.$refs.modForm.switch('userName', 'disabled', true);
                    me.$refs.modForm.switch('userKey', 'disabled', true);
                    me.$refs.modForm.switch('password', 'disabled', true);
                    me.$refs.modForm.switch('pwdKey', 'disabled', true);
                  }
                }
              },
              {
                field: 'userName',
                label: this.$t('functionManage.wbxtdlm'),
                disabled: true,
                rules: [
                  { max: 50, message: this.$t('functionManage.srzgc'), trigger: 'blur' },
                  { validator: yufp.validator.speChar, message: this.$t('functionManage.srxxbhtszf'), trigger: 'blur' }
                ]
              },
              {
                field: 'userKey',
                label: this.$t('functionManage.wbxtyhm'),
                disabled: true,
                rules: [
                  { max: 50, message: this.$t('functionManage.srzgc'), trigger: 'blur' },
                  { validator: yufp.validator.speChar, message: this.$t('functionManage.srxxbhtszf'), trigger: 'blur' }
                ]
              },
              {
                field: 'password',
                label: this.$t('functionManage.wbxtmm'),
                disabled: true,
                rules: [
                  { max: 50, message: this.$t('functionManage.srzgc'), trigger: 'blur' }
                ]
              },
              {
                field: 'pwdKey',
                label: this.$t('functionManage.wbxtmmm'),
                disabled: true,
                rules: [
                  { max: 50, message: this.$t('functionManage.srzgc'), trigger: 'blur' }
                ]
              },
              { field: 'lastChgName', label: this.$t('functionManage.zjgxr'), hidden: true },
              { field: 'lastChgDt', label: this.$t('functionManage.zjgxsj'), hidden: true }
            ]
          }, {
            columnCount: 1,
            fields: [
              {
                field: 'modDesc',
                label: this.$t('functionManage.mkms'),
                type: 'textarea',
                rows: 3,
                rules: [
                  { max: 100, message: this.$t('functionManage.srzgc'), trigger: 'blur' }
                ]
              }
            ]
          }],
          funcFields: [
            {
              columnCount: 1,
              fields: [
                {
                  field: 'funcName',
                  label: this.$t('functionManage.gndmc'),
                  rules: [
                    { required: true, message: this.$t('functionManage.btx'), trigger: 'blur' },
                    { max: 50, message: this.$t('functionManage.srzgc'), trigger: 'blur' },
                    { validator: yufp.validator.speChar, message: this.$t('functionManage.srxxbhtszf'), trigger: 'blur' }
                  ]
                },
                {
                  field: 'funcUrl',
                  label: this.$t('functionManage.lj'),
                  rules: [
                    { required: true, message: this.$t('functionManage.btx'), trigger: 'blur' },
                    { max: 200, message: this.$t('functionManage.srzgc'), trigger: 'blur' },
                    { validator: urlValidate, trigger: 'blur' }
                  ],
                  placeholder: this.$t('functionManage.lr')
                }
              ]
            },
            {
              columnCount: 2,
              fields: [
                {
                  field: 'funcIcon',
                  label: this.$t('functionManage.tb'),
                  icon: 'search',
                  readonly: true,
                  click: function () {
                    me.iconDialogVisible = true;
                  },
                  rules: [{ required: true, message: this.$t('functionManage.btx'), trigger: 'blur' }]
                },
                {
                  field: 'funcOrder',
                  label: this.$t('functionManage.sx'),
                  rules: [
                    { required: true, message: this.$t('functionManage.btx') },
                    { validator: orderValidate, trigger: 'blur' }
                  ]
                },
                { field: 'lastChgName', label: this.$t('functionManage.zjgxr'), hidden: true },
                { field: 'lastChgDt', label: this.$t('functionManage.zjgxsj'), hidden: true }
              ]
            }, {
              columnCount: 1,
              fields: [{
                field: 'funcDesc',
                label: this.$t('functionManage.gndms'),
                type: 'textarea',
                rows: 3,
                rules: [{ max: 100, message: this.$t('functionManage.srzgc'), trigger: 'blur' }]
              }]
            }],
          modFormButtons: [
            {
              label: this.$t('functionManage.bc'),
              type: 'primary',
              hidden: false,
              op: 'submit',
              click: function (model) {
                if (me.$refs.modForm.formModel.isOuter == 'N') {
                  me.isOuterFn();
                }
                me.saveCreateMod();
              }
            },
            {
              label: this.$t('functionManage.bc'),
              type: 'primary',
              hidden: false,
              op: 'submit',
              click: function (model) {
                if (me.$refs.modForm.formModel.isOuter == 'N') {
                  me.isOuterFn();
                }
                me.saveEditMod();
              }
            },
            {
              label: this.$t('functionManage.qx'),
              hidden: false,
              click: function (model) {
                me.modDialogVisible = false;
              }
            }],
          funcFormButtons: [
            {
              label: this.$t('functionManage.bc'),
              type: 'primary',
              hidden: false,
              op: 'submit',
              click: function (model) {
                me.saveCreateFunc();
              }
            },
            {
              label: this.$t('functionManage.bc'),
              type: 'primary',
              hidden: false,
              op: 'submit',
              click: function (model) {
                me.saveEditFunc();
              }
            },
            {
              label: this.$t('functionManage.qx'),
              hidden: false,
              click: function (model) {
                me.funcDialogVisible = false;
              }
            }
          ]
        };
      },
      methods: {
        // 模块管理操作状态选择
        modSwitchStatus: function (viewType, editable) {
          this.viewType = viewType;
          this.modDialogVisible = true;
          this.modFormDisabled = !editable;
          this.modFormButtons[2].hidden = !editable;
          if (viewType == 'ADD') {
            this.modFormButtons[0].hidden = !editable;
            this.modFormButtons[1].hidden = editable;
          } else if (viewType == 'EDIT') {
            this.modFormButtons[0].hidden = editable;
            this.modFormButtons[1].hidden = !editable;
          } else if (viewType == 'DETAIL') {
            this.modFormButtons[0].hidden = !editable;
            this.modFormButtons[1].hidden = !editable;
          }
        },
        // 模块管理不同状态最近更新人与时间字段显示控制
        modSwitchHidden: function (viewType, isHidden) {
          this.viewType = viewType;
          var fields = this.modFields[1].fields;
          fields[6].hidden = !isHidden;
          fields[7].hidden = !isHidden;
        },
        // 是否外部系统
        isOuterFn: function () {
          this.$refs.modForm.formModel.userName = '';
          this.$refs.modForm.formModel.userKey = '';
          this.$refs.modForm.formModel.password = '';
          this.$refs.modForm.formModel.pwdKey = '';
        },
        // 业务功能管理操作状态选择
        funcSwitchStatus: function (viewType, editable) {
          this.viewType = viewType;
          this.funcDialogVisible = true;
          this.funcFormDisabled = !editable;
          this.funcFormButtons[2].hidden = !editable;
          if (viewType == 'ADD') {
            this.funcFormButtons[0].hidden = !editable;
            this.funcFormButtons[1].hidden = editable;
          } else if (viewType == 'EDIT') {
            this.funcFormButtons[0].hidden = editable;
            this.funcFormButtons[1].hidden = !editable;
          } else if (viewType == 'DETAIL') {
            this.funcFormButtons[0].hidden = !editable;
            this.funcFormButtons[1].hidden = !editable;
          }
        },
        // 业务功能管理不同状态最近更新人与时间字段显示控制
        funcSwitchHidden: function (viewType, isHidden) {
          this.viewType = viewType;
          var fields = this.funcFields[1].fields;
          fields[2].hidden = !isHidden;
          fields[3].hidden = !isHidden;
        },
        // 模块新增
        modAddFn: function () {
          this.modSwitchStatus('ADD', true);
          this.$refs.modTable.clearSelection();
          this.modSwitchHidden('ADD', false);
          this.$nextTick(function () {
            this.$refs.modForm.resetFn();
            this.$refs.modForm.formModel.isApp = 'N';
            this.$refs.modForm.formModel.isOuter = 'N';
            this.$refs.modForm.rebuildFn();
          });
        },
        saveCreateMod: function () {
          var _this = this;
          delete _this.$refs.modForm.formModel.modId;
          _this.$refs.modForm.validate(function (valid) {
            if (valid) {
              _this.$refs.modForm.formModel.lastChgUsr = yufp.session.userId;
              yufp.service.request({
                method: 'GET',
                url: backend.appOcaService + '/api/adminsmfuncmod/checkname',
                data: {
                  modName: _this.$refs.modForm.formModel.modName ? _this.$refs.modForm.formModel.modName : null,
                  modId: _this.$refs.modForm.formModel.modId ? _this.$refs.modForm.formModel.modId : null
                },
                callback: function (code, message, response) {
                  if (response.data.length > 0) {
                    _this.$message({ message: _this.$t('functionManage.gmkmcyczqxg'), type: 'warning' });
                    return;
                  } else {
                    yufp.service.request({
                      method: 'POST',
                      url: backend.appOcaService + '/api/adminsmfuncmod/createmod',
                      data: _this.$refs.modForm.formModel,
                      callback: function (code, message, response) {
                        if (code == '0' && response.code == 0) {
                          _this.modDialogVisible = false;
                          _this.$message({ message: _this.$t('functionManage.sjbccg') });
                          _this.$refs.modTable.remoteData();
                        }
                      }
                    });
                  }
                }
              });
            } else {
              _this.$message({ message: _this.$t('functionManage.qjcsrxsfhf'), type: 'warning' });
              return false;
            }
          });
        },
        // 模块修改
        modModifyFn: function () {
          if (this.$refs.modTable.selections.length < 1) {
            this.$message({ message: this.$t('functionManage.qxxzytjl'), type: 'warning' });
            return;
          }
          this.modSwitchStatus('EDIT', true);
          this.modSwitchHidden('EDIT', false);
          this.$nextTick(function () {
            this.$refs.modForm.formModel = this.$refs.modForm.formModel ? yufp.extend({}, this.$refs.modTable.selections[0]) : this.$refs.modForm.formModel;
          });
        },
        saveEditMod: function () {
          var _this = this;
          _this.$refs.modForm.validate(function (valid) {
            if (valid) {
              _this.$refs.modForm.formModel.lastChgUsr = yufp.session.userId;
              yufp.service.request({
                method: 'GET',
                url: backend.appOcaService + '/api/adminsmfuncmod/checkname',
                data: {
                  modName: _this.$refs.modForm.formModel.modName ? _this.$refs.modForm.formModel.modName : null,
                  modId: _this.$refs.modForm.formModel.modId ? _this.$refs.modForm.formModel.modId : null
                },
                callback: function (code, message, response) {
                  if (response.data.length > 0) {
                    _this.$message({ message: _this.$t('functionManage.gmkmcyczqxg'), type: 'warning' });
                    return;
                  } else {
                    yufp.service.request({
                      method: 'POST',
                      url: backend.appOcaService + '/api/adminsmfuncmod/editmod',
                      data: _this.$refs.modForm.formModel,
                      callback: function (code, message, response) {
                        if (code == '0' && response.code == 0) {
                          _this.modDialogVisible = false;
                          _this.$message({ message: _this.$t('functionManage.sjbccg') });
                          _this.$refs.modTable.remoteData();
                        }
                      }
                    });
                  }
                }
              });
            } else {
              _this.$message({ message: _this.$t('functionManage.qjcsrxsfhf'), type: 'warning' });
              return false;
            }
          });
        },
        // 业务功能新增
        funcAddFn: function () {
          if (this.modCurrentRow.modId == '' || this.modCurrentRow.modId == undefined) {
            this.$message({ message: this.$t('functionManage.qxxzmk'), type: 'warning' });
            return;
          }
          this.funcSwitchStatus('ADD', true);
          this.funcSwitchHidden('ADD', false);
          this.$refs.funcTable.clearSelection();
          this.$nextTick(function () {
            this.$refs.funcForm.resetFn();
          });
        },
        saveCreateFunc: function () {
          var _this = this;
          delete _this.$refs.funcForm.formModel.funcId;
          _this.$refs.funcForm.validate(function (valid) {
            _this.$refs.funcForm.formModel.modId = _this.modCurrentRow.modId;
            _this.$refs.funcForm.formModel.lastChgUsr = yufp.session.userId;
            if (valid) {
              yufp.service.request({
                method: 'POST',
                url: backend.appOcaService + '/api/adminsmbusifunc/createfunc',
                data: _this.$refs.funcForm.formModel,
                callback: function (code, message, response) {
                  if (code == '0' && response.code == 0) {
                    _this.funcDialogVisible = false;
                    _this.$message({ message: _this.$t('functionManage.sjbccg') });
                    var param = {
                      condition: JSON.stringify({
                        modId: _this.modCurrentRow.modId ? _this.modCurrentRow.modId : null
                      })
                    };
                    _this.$refs.funcTable.remoteData(param);
                  } else if (response && response.code == 2001) {
                    _this.$message({ message: _this.$t('functionManage.ljgsbzq'), type: 'warning' });
                  }
                }
              });
            } else {
              _this.$message({ message: _this.$t('functionManage.qjcsrxsfhf'), type: 'warning' });
              return false;
            }
          });
        },
        // 业务功能修改
        funcModifyFn: function () {
          if (this.$refs.funcTable.selections.length < 1) {
            this.$message({ message: this.$t('functionManage.qxxzytjl'), type: 'warning' });
            return;
          }
          if (this.$refs.funcTable.selections.length > 1) {
            this.$message({ message: this.$t('functionManage.qxzytjl'), type: 'warning' });
            return;
          }
          this.funcSwitchStatus('EDIT', true);
          this.funcSwitchHidden('EDIT', false);
          this.$nextTick(function () {
            this.$refs.funcForm.resetFn();
            yufp.extend(this.$refs.funcForm.formModel, this.$refs.funcTable.selections[0]);
          });
        },
        saveEditFunc: function () {
          var _this = this;
          _this.$refs.funcForm.validate(function (valid) {
            _this.$refs.funcForm.formModel.lastChgUsr = yufp.session.userId;
            if (valid) {
              yufp.service.request({
                method: 'POST',
                url: backend.appOcaService + '/api/adminsmbusifunc/editfunc',
                data: _this.$refs.funcForm.formModel,
                callback: function (code, message, response) {
                  if (code == '0' && response.code == 0) {
                    _this.funcDialogVisible = false;
                    _this.$message({ message: _this.$t('functionManage.sjbccg') });
                    var param = {
                      condition: JSON.stringify({
                        modId: _this.modCurrentRow.modId ? _this.modCurrentRow.modId : null
                      })
                    };
                    _this.$refs.funcTable.remoteData(param);
                  } else if (response && response.code == 2001) {
                    _this.$message({ message: _this.$t('functionManage.ljgsbzq'), type: 'warning' });
                  }
                }
              });
            } else {
              _this.$message({ message: _this.$t('functionManage.qjcsrxsfhf'), type: 'warning' });
              return false;
            }
          });
        },
        // 点击模块名称列,查看模块管理详情
        modRowClick: function (scope) {
          this.modSwitchStatus('DETAIL', false);
          this.modSwitchHidden('DETAIL', true);
          this.$nextTick(function () {
            this.$refs.modForm.resetFn();
            yufp.extend(this.$refs.modForm.formModel, scope.row);
          });
        },
        // 点击业务功能名称列,查看业务功能管理详情
        funcRowClick: function (scope) {
          this.funcSwitchStatus('DETAIL', false);
          this.funcSwitchHidden('DETAIL', true);
          this.$nextTick(function () {
            this.$refs.funcForm.resetFn();
            yufp.extend(this.$refs.funcForm.formModel, scope.row);
          });
        },
        // 模块删除,删除前先判断是否有关联业务功能信息,如有不能进行删除
        modDeleteFn: function () {
          if (this.$refs.modTable.selections.length < 1) {
            this.$message({ message: this.$t('functionManage.qxxzytjl'), type: 'warning' });
            return;
          }
          var _this = this;
          _this.$confirm(_this.$t('functionManage.qdscsxmk'), _this.$t('functionManage.ts'), {
            confirmButtonText: _this.$t('functionManage.qd'),
            cancelButtonText: _this.$t('functionManage.qx'),
            type: 'warning'
          }).then(function () {
            yufp.service.request({
              method: 'POST',
              data: {
                modId: _this.$refs.modTable.selections[0].modId
              },
              url: backend.appOcaService + '/api/adminsmfuncmod/deletemod',
              callback: function (code, message, response) {
                if (code == '0' && response.code == 0) {
                  _this.$message({ message: _this.$t('functionManage.sjsccg') });
                  _this.modCurrentRow.modId = '';
                  _this.$refs.modTable.remoteData({});
                } else {
                  _this.$message({ message: response.message, type: 'warning' });
                }
              }
            });
          }).catch(function () {
            return;
          });
        },
        // 业务功能删除,删除前先判断是否有关联控制点信息,如有不能进行删除
        funcDeleteFn: function () {
          if (this.$refs.funcTable.selections.length < 1) {
            this.$message({ message: this.$t('functionManage.qxxzytjl'), type: 'warning' });
            return;
          }
          var _this = this;
          if (_this.$refs.funcTable.selections) {
            var ids = '';
            for (var i = 0; i < _this.$refs.funcTable.selections.length; i++) {
              ids = ids + ',' + _this.$refs.funcTable.selections[i].funcId;
            }
            _this.$confirm(_this.$t('functionManage.qdscsxywgn'), _this.$t('functionManage.ts'), {
              confirmButtonText: _this.$t('functionManage.qd'),
              cancelButtonText: _this.$t('functionManage.qx'),
              type: 'warning'
            }).then(function () {
              yufp.service.request({
                method: 'POST',
                data: {
                  id: ids
                },
                url: backend.appOcaService + '/api/adminsmbusifunc/deletefunc',
                callback: function (code, message, response) {
                  if (code == '0') {
                    _this.$message({ message: response.message });
                    var param = {
                      condition: JSON.stringify({
                        modId: _this.modCurrentRow.modId ? _this.modCurrentRow.modId : null
                      })
                    };
                    _this.$refs.funcTable.remoteData(param);
                  }
                }
              });
            }).catch(function () {
              return;
            });
          }
        },
        // 模块点击事件,查询模块关联业务功能
        modSelect: function (row, event) {
          this.modCurrentRow = row;
          var param = {
            condition: JSON.stringify({
              modId: this.modCurrentRow.modId ? this.modCurrentRow.modId : null
            })
          };
          this.$refs.funcTable.remoteData(param);
        },
        // 业务功能图标选择
        iconSelect: function (event) {
          this.$refs.funcForm.formModel.funcIcon = event.target.className;
          this.iconDialogVisible = false;
        }
      }
    });
  };
});
